#include "speedtest.h"
#include "ui_speedtest.h"
#include "qmessagebox.h"


SpeedTest::SpeedTest(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::SpeedTest)
{
    ui->setupUi(this);

    connect(&timer, SIGNAL(timeout()), this, SLOT(timerHandle()));

    bNappi[0] = bNappi[1] = bNappi[2] = bNappi[3] = false;
    bNappiActive = false;
    score = 0;
    bGameOn = false;
    nappiLast = 0;
    delay = 1000.0f;

    color_on[0] = QColor(0, 255, 0);
    color_on[2] = QColor(0, 0, 255);
    color_on[1] = QColor(255, 0, 0);
    color_on[3] = QColor(255, 255, 0);

    color_off[0] = QColor(0,40, 0);
    color_off[2] = QColor(0, 0, 40);
    color_off[3] = QColor(40, 40, 0);
    color_off[1] = QColor(40, 0, 0);

    taustaImage = QImage(":/images/back.jpg");


}

SpeedTest::~SpeedTest()
{
    delete ui;
}

void SpeedTest::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void SpeedTest::showEvent ( QShowEvent * event )
{
   update();
}

// Initializes all the variables needed in a new game

void SpeedTest::newGame()
{
    bNappi[0] = bNappi[1] = bNappi[2] = bNappi[3] = false;
    nappiNext.clear();
    bNappiActive = false;
    score = 0;
    delay = 1000.0f;
    bGameOn = true;
    ui->lcdNumber->display(0);
    highScores.reset();
    highScores.hide();
    update();
    timer.start(1300);
}


//The hit to button is checked from mouse button release event
//Adds score

void SpeedTest::mouseReleaseEvent ( QMouseEvent *e )
{
    if (bGameOn)
    {
        for (int i=0;i<4;i++)
        {
            if (e->x() > (30+i*150) && e->x() < (160+i*150) && e->y()>150 && e->y()<280)
            {
                if (nappiNext.isEmpty() || i!=nappiNext[0]) gameOver();

                else if (!nappiNext.isEmpty() && nappiNext[0] == i && bNappiActive)
                {                  
                    nappiNext.removeFirst();
                    score++;
                    ui->lcdNumber->display(score);
                    bNappi[i] = false;
                    if (nappiNext.isEmpty()) bNappiActive = false;
                }
                //if (nappi[i]) nappi[i]=false;
            }
        }
        update();
    }
}


// Sets new active button.
// Also checks that the button is different than previous one
//so there's no consecutive buttons shown.
void SpeedTest::updateNappi()
{

    int nn = rand() % 4;
    while (nn == nappiLast) nn = rand() % 4;

    if (nappiNext.isEmpty() || !bNappiActive)
        nappiNext.append(nn);
    else if (bNappiActive)
    {
        while (nn == nappiNext[nappiNext.length()-1]) nn = rand() % 4;
        nappiNext.append(nn);
    }
    if (!bNappiActive) bNappi[nappiLast] = false;
    nappiLast = nn;

}

// Timer runs out time when it's time to light another button

void SpeedTest::timerHandle()
{
    timer.stop();

    if (bNappiActive && bGameOn){
        if (nappiNext.isEmpty()) updateNappi();
        else if (!nappiNext.isEmpty() && nappiNext.length() > 5 && bNappiActive) gameOver();
        else updateNappi();
    }
    else {       
        updateNappi();
        bNappiActive = true;
    }

    update();

    delay -= (float)(130.00f*(1.00f/(((float)score+2.00f))));
    timer.start((int)delay);

}

void SpeedTest::paintEvent ( QPaintEvent *e )
{
    QPainter painter(this);
    painter.setPen(QPen(QColor(150,0,0)));
    painter.drawImage(QPoint(0, 0), taustaImage);

    // Draws the buttons
    for (int i=0; i<4; i++)
    {
        if (!bNappi[i])
        {
            painter.setBrush(QBrush(color_off[i], Qt::SolidPattern));
        }
        else
        {
            painter.setBrush(QBrush(color_on[i], Qt::SolidPattern));          
        }
        painter.drawEllipse(30+(i*150), 150, 130, 130);
    }

    // Draws the current highlighted button
    if (bNappiActive)
    {
        painter.setBrush(QBrush(color_on[nappiNext[nappiNext.length()-1]], Qt::SolidPattern));
        painter.drawEllipse(30+(nappiNext[nappiNext.length()-1]*150), 150, 130, 130);
    }

    if (highScores.isShown()) highScores.draw(&painter);


}

void SpeedTest::gameOver()
{
    timer.stop();

    highScores.load("scores.txt");
    highScores.addScore(score, this);
    highScores.save("scores.txt");
    highScores.show();

    bNappi[0] = bNappi [1] = bNappi[2] = bNappi[3] = true;
    bNappiActive = false;
    bGameOn = false;
    nappiNext.clear();
    highScores.show();
    update();
}



void SpeedTest::on_pushButton_clicked()
{
    newGame();
}

void SpeedTest::on_buttonExit_clicked()
{
    exit(0);
}
